Mapa de la quinta entrega

Mapa de la quinta entrega#

import pandas as pd
import warnings
import seaborn as sns
import matplotlib.pyplot as plt
from tabulate import tabulate
from numpy import mean
import geopandas as gpd
import pandas as pd
import folium
from folium import Choropleth, Marker
from IPython.display import display
from folium.plugins import HeatMap

warnings.filterwarnings('ignore')
t32023 = pd.read_csv("precios_t32023.csv")
t42023 = pd.read_csv("precios_t42023.csv")
t12023 = pd.read_csv("precios_t12023.csv")
t22023 = pd.read_csv("precios_t22023.csv")

d2023 = pd.concat([t32023, t42023, t12023, t22023], ignore_index=True)
d2023.head()

mapa_col = gpd.read_file("COLOMBIA.shp")
mapa_col.head()

mapa_col['DPTO_CNMBR'] = mapa_col['DPTO_CNMBR'].str.replace('?', 'Ñ')
mapa_col['DPTO_CNMBR'] = mapa_col['DPTO_CNMBR'].str.replace('ARCHIPIELAGO DE SAN ANDRES, SANTA CATALINA Y PROVIDENCIA', 'ARCHIPIELAGO DE SAN ANDRES')

mapa_col = mapa_col.rename(columns={'DPTO_CNMBR': 'DEPARTAMENTO'})
precios_promedio = d2023.groupby('DEPARTAMENTO')['VALOR PRECIO'].mean().reset_index()
precios_promedio = precios_promedio.rename(columns={'VALOR PRECIO': 'PROMEDIO_PRECIO'})
datos_unidos = mapa_col.merge(precios_promedio, on='DEPARTAMENTO', how='left')
mapa_col.head(33)
OBJECTID DPTO_CCDGO DPTO_NANO_ DEPARTAMENTO DPTO_CACTO DPTO_NAREA DPTO_CSMBL DPTO_NANO PAIS_PAIS_ SHAPE_Leng SHAPE_Area geometry
0 1 05 1886 ANTIOQUIA Constitucion Politica de 1886 6.306333e+10 3 2005 1 21.137035 5.155783 POLYGON ((-76.40481 8.85708, -76.4044 8.85625,...
1 2 08 1910 ATLANTICO Ley 21 de 1910 3.326730e+09 3 2005 2 2.461077 0.274825 POLYGON ((-74.82969 11.04992, -74.82937 11.049...
2 3 11 0 BOGOTA D.C. None 1.633209e+09 3 2005 3 3.731288 0.133045 POLYGON ((-74.07274 4.83565, -74.07217 4.83505...
3 4 13 1886 BOLIVAR Constitucion Politica de 1886 2.666559e+10 3 2005 4 15.706980 2.191055 MULTIPOLYGON (((-75.24966 10.79804, -75.24926 ...
4 5 15 1886 BOYACA Constitucion Politica de 1886 2.307705e+10 3 2005 5 15.280968 1.883315 POLYGON ((-72.01129 7.00944, -72.01029 7.00912...
5 6 17 1905 CALDAS 11 de Abril de 1905 7.415933e+09 3 2005 6 6.614200 0.604712 POLYGON ((-74.67118 5.77127, -74.67112 5.77115...
6 7 18 1981 CAQUETA Ley 78 del 29 de Diciembre de 1981 9.007665e+10 3 2005 7 19.850907 7.316017 POLYGON ((-74.91094 2.96445, -74.90768 2.96367...
7 8 19 1857 CAUCA 15 de junio de 1857 3.064331e+10 3 2005 8 13.352092 2.485633 POLYGON ((-76.45841 3.32857, -76.45819 3.32857...
8 9 20 1967 CESAR Ley 25 21 de junio de 1967 2.228367e+10 3 2005 9 12.597031 1.834978 POLYGON ((-73.45957 10.86878, -73.45905 10.868...
9 10 23 1951 CORDOBA Ley 9 del 18 de Diciembre de 1951 2.504194e+10 3 2005 10 8.982757 2.053760 POLYGON ((-75.92261 9.43892, -75.92209 9.43846...
10 11 25 1886 CUNDINAMARCA Constitucion Politica de 1886 2.236838e+10 3 2005 11 12.960193 1.823363 POLYGON ((-73.05116 4.735, -73.05862 4.68673, ...
11 12 27 1947 CHOCO Ley 13 del 3 de Noviembre de 1947 4.757908e+10 3 2005 12 19.085028 3.876418 POLYGON ((-77.347 8.64468, -77.34604 8.64467, ...
12 13 41 1905 HUILA Ley 46 de 1905 1.871102e+10 3 2005 13 9.803136 1.520385 POLYGON ((-74.52051 3.82768, -74.51933 3.82763...
13 14 44 1964 LA GUAJIRA Acto Legislativo No. 1 de Diciembre 28 de 1964 2.067067e+10 3 2005 14 10.168370 1.711062 POLYGON ((-71.66688 12.4582, -71.66673 12.4581...
14 15 47 0 MAGDALENA None 2.320573e+10 3 2005 15 10.747550 1.914917 POLYGON ((-74.04415 11.34911, -74.04397 11.349...
15 16 50 1959 META Ley 118 del 16 de Diciembre de 1959 8.548555e+10 3 2005 16 17.455890 6.951611 POLYGON ((-71.0775 4.92462, -71.07753 4.87697,...
16 17 52 1904 NARIÑO Ley 1 de 1904 3.164991e+10 3 2005 17 10.819582 2.560667 POLYGON ((-78.00558 2.68956, -78.00547 2.68956...
17 18 54 1910 NORTE DE SANTANDER Ley 25 de 1910 2.202302e+10 3 2005 18 11.128765 1.805850 POLYGON ((-73.00472 9.28011, -73.0052 9.27873,...
18 19 63 1966 QUINDIO Ley 2 TM de 1966 1.935384e+09 3 2005 19 2.507765 0.157564 POLYGON ((-75.7024 4.72112, -75.70211 4.72063,...
19 20 66 1966 RISARALDA Ley 70 del 1 de Diciembre de 1966 3.978381e+09 3 2005 20 5.135623 0.324123 POLYGON ((-75.99569 5.52893, -75.99512 5.52748...
20 21 68 1910 SANTANDER Ley 25 14 de Julio de 1910 3.059683e+10 3 2005 21 11.452211 2.501891 POLYGON ((-73.77891 8.13972, -73.77701 8.14216...
21 22 70 1966 SUCRE Ley 47 del 8 de Agosto de 1966 1.070915e+10 3 2005 22 8.133088 0.880406 POLYGON ((-75.53687 10.06265, -75.54466 10.045...
22 23 73 1909 TOLIMA Ley 65 de Noviembre de 1909 2.398336e+10 3 2005 23 9.315385 1.952249 POLYGON ((-74.7336 5.28577, -74.73033 5.27905,...
23 24 76 1910 VALLE DEL CAUCA Decreto No 340 de 16 de Abril de 1910 2.126936e+10 3 2005 24 10.439368 1.728411 POLYGON ((-76.06306 5.01515, -76.0624 5.0151, ...
24 25 81 1991 ARAUCA 5 de Julio Constitucion Politica de 1991 2.380556e+10 3 2005 25 9.162939 1.940327 POLYGON ((-70.67671 7.09263, -70.67666 7.09251...
25 26 85 1991 CASANARE 5 de Julio Constitucion Politica de 1991 4.448400e+10 3 2005 26 11.830742 3.622186 POLYGON ((-72.36091 6.28102, -72.36093 6.28101...
26 27 86 1991 PUTUMAYO Articulo 309 Constitucion Politica de 1991 2.589376e+10 3 2005 27 12.672464 2.101163 POLYGON ((-76.89592 1.51027, -76.89606 1.50996...
27 28 91 1991 AMAZONAS Decreto 2274 del 4 de Octubre de la Constituci... 1.100465e+11 3 2005 28 24.747842 8.921607 POLYGON ((-71.29212 0.06821, -71.28829 0.06762...
28 29 94 1991 GUAINIA Articulo 309 Constitucion Politica de 1991 7.156745e+10 3 2005 29 19.493315 5.769826 POLYGON ((-67.68356 3.90772, -67.6795 3.90308,...
29 30 95 1991 GUAVIARE 5 de Julio Constitucion Politica de 1991 5.545392e+10 3 2005 30 18.739054 4.501348 POLYGON ((-71.28567 2.87055, -71.285 2.87051, ...
30 31 97 1991 VAUPES Articulo 309 Constitucion Politica de 1991 5.354358e+10 3 2005 31 19.242591 4.333330 POLYGON ((-70.10256 2.07311, -70.10401 2.07224...
31 32 99 1991 VICHADA 5 de Julio Constitucion Politica de 1991 1.000144e+11 3 2005 32 17.181693 8.096236 POLYGON ((-67.7098 4.03949, -67.71273 4.03756,...
32 33 88 1991 ARCHIPIELAGO DE SAN ANDRES Artículo 310 Constitucion Politica de 1991 4.960214e+04 3 2005 12 0.650698 0.004064 MULTIPOLYGON (((-81.70353 12.59404, -81.70342 ...
m = folium.Map(location=[4.5709, -74.2973], zoom_start=7, tiles='CartoDB positron')

folium.Choropleth(
    geo_data=datos_unidos,
    name='choropleth',
    data=datos_unidos,
    columns=['DEPARTAMENTO', 'PROMEDIO_PRECIO'],
    key_on='feature.properties.DEPARTAMENTO',
    fill_color='Spectral',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Precio Promedio',
    highlight=True,
).add_to(m)

for _, row in datos_unidos.iterrows():
    folium.Marker(
        location=[row.geometry.centroid.y, row.geometry.centroid.x],
        icon=None,
        popup=f"{row['DEPARTAMENTO']}: {row['PROMEDIO_PRECIO']:.2f}",
    ).add_to(m)

#j
folium.LayerControl().add_to(m)

m
Make this Notebook Trusted to load map: File -> Trust Notebook